var timer = null;

function searchPeopleTimer(search, inputToBeChecked) {
	$("#userSearchResult").empty();
	clearTimeout(timer);
	search = trimSearch(search);
	if (search != null && search.length > 2) {
		timer = setTimeout('searchPeople(\''+search+'\', \''+inputToBeChecked+'\');', 800);
	}
}

function searchPeople(search, inputToBeChecked) {
	if ($("#"+inputToBeChecked).val() == search) {
		$.ajax({
			type : "GET",
			url : "getusers",
			dataType : "json",
			data : {
				search : search
			},
			success : function(returnValue) {
				$("#userSearchResult").empty();
				if (returnValue != null) {
					if (returnValue.result != null && returnValue.result.length > 0) {
						$.jStorage.set('userSearchResult', returnValue.result);
						var i = 0;
						for (i = 0; i < returnValue.result.length; i++) {
							$("#userSearchResult").append(printUserResult(returnValue.result[i], i));
						}
					} else {
						$("#userSearchResult").append('<p><i class="fa fa-frown-o"></i> Aucun r&eacute;sultat pour votre recherche.</p>');
					}
				}
				return returnValue; // with return value excecute code
			},
			error : function(request, error) {
				alert('error');
			}
		});
	}
}

function searchModuleTimer(search, inputToBeChecked) {
	$("#moduleSearchRowResult").empty();
	clearTimeout(timer);
	search = trimSearch(search);
	if (search != null && search.length >= 2) {
		timer = setTimeout('searchModule(\''+search+'\', \''+inputToBeChecked+'\');', 800);
	}
}

function searchModule(search, inputToBeChecked) {
	if($("#"+inputToBeChecked).val() == search) {
		$.ajax({
			type : "GET",
			url : "searchModule",
			dataType : "json",
			data : {
				search : search
			},
			success : function(returnValue) {
				$("#moduleSearchRowResult").empty();
				if(returnValue != null && returnValue.result != null && returnValue.result.length > 0){
					$.jStorage.set('searchedModules', returnValue.result);
					for(var i = 0; i<returnValue.result.length; i++){
						$("#moduleSearchRowResult").append(
								printModuleResult(returnValue.result[i], i, 'searchedModules'));
					}
				} else {
					$("#moduleSearchRowResult").append('<p><i class="fa fa-frown-o"></i> Aucun r&eacute;sultat pour votre recherche.</p>');
				}
				return returnValue; // with return value excecute code
			},
			error : function(request, error) {
				alert('error');
			}
		});
	}
}

function searchGroupTimer(search, inputToBeChecked) {
	$("#groupSearchRowResult").empty();
	clearTimeout(timer);
	search = trimSearch(search);
	if (search != null && search.length >= 2) {
		timer = setTimeout('searchGroup(\''+search+'\', \''+inputToBeChecked+'\');', 800);
	}
}

function searchGroup(search, inputToBeChecked) {
	if($("#"+inputToBeChecked).val() == search) {
		$.ajax({
			type : "GET",
			url : "searchGroup",
			dataType : "json",
			data : {
				search : search
			},
			success : function(returnValue) {
				$("#groupSearchRowResult").empty();
				if(returnValue != null && returnValue.result != null && returnValue.result.length > 0){
					$.jStorage.set('searchedGroups', returnValue.result);
					for(var i = 0; i<returnValue.result.length; i++){
						$("#groupSearchRowResult").append(
								printGroupResult(returnValue.result[i], i, 'searchedGroups'));
					}
				} else {
					$("#groupSearchRowResult").append('<p><i class="fa fa-frown-o"></i> Aucun r&eacute;sultat pour votre recherche.</p>');
				}
				return returnValue; // with return value excecute code
			},
			error : function(request, error) {
				alert('error');
			}
		});
	}
}

function searchRenderTimer(search, inputToBeChecked) {
	$("#renderSearchRowResult").empty();
	clearTimeout(timer);
	search = trimSearch(search);
	if (search != null && search.length >= 2) {
		timer = setTimeout('searchRender(\''+search+'\', \''+inputToBeChecked+'\');', 800);
	}
}

function searchRender(search, inputToBeChecked) {
	if($("#"+inputToBeChecked).val() == search) {
		$.ajax({
			type : "GET",
			url : "searchRender",
			dataType : "json",
			data : {
				search : search
			},
			success : function(returnValue) {
				$("#renderSearchRowResult").empty();
				if(returnValue != null && returnValue.result != null && returnValue.result.length > 0){
					$.jStorage.set('searchedRenders', returnValue.result);
					for(var i = 0; i<returnValue.result.length; i++){
						$("#renderSearchRowResult").append(
								printRenderResult(returnValue.result[i], i, 'searchedRenders'));
					}
				} else {
					$("#renderSearchRowResult").append('<p><i class="fa fa-frown-o"></i> Aucun r&eacute;sultat pour votre recherche.</p>');
				}
				return returnValue; // with return value excecute code
			},
			error : function(request, error) {
				alert('error');
			}
		});
	}
}

// Like trim() in Java.
function trimSearch(search) {
	return search.replace(/^\s+/g, '').replace(/\s+$/g, '');
};